package spitfire.ksim.view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.List;

import javax.swing.JPanel;

import spitfire.ksim.node.KSensor;
import spitfire.ksim.node.KSensorNode;
import spitfire.ksim.simulator.KSimulator;

/**
 * 
 */
public class KSensorDataPanel extends JPanel {
	
	private static final long serialVersionUID = -8526652820599889584L;
	private KSimulator simulator;
//	private KSensor sensor;
	private List<Long> timeList;
	private List<Double> dataList;
	private double rangeUpper=500;
	private double rangeLower=0;

	public KSensorDataPanel(KSensor sensor, KSimulator simulator) {
		super();
//		this.sensor = sensor;
		this.simulator = simulator;
		timeList = sensor.getTimeStampList();
		dataList = sensor.getDataList();
		if (dataList.size() != 0) {
			rangeLower = Collections.min(dataList);
			rangeUpper = Collections.max(dataList);
		}
		
		setBackground(Color.WHITE);
		setPreferredSize(new Dimension(300, 100));
	}

	@Override 
	public void paintComponent(Graphics g) {
 		super.paintComponent(g);
 		int height = getHeight();
 		int width = getWidth();
 		
 		// Draw axis
		DecimalFormat f = new DecimalFormat("0.00");
		g.setColor(Color.BLACK);
		//g.drawLine(WIDTH/2+50, 0, WIDTH/2+50, HEIGHT);
		g.drawLine(50, 0, 50, height + 50);
		g.drawLine(0, height, width + 50, height);
		int portion = 10;
		for (int j = 0; j <= portion; j++) {
			double value = (rangeLower + ((rangeUpper - rangeLower) / portion) * j);
			int valueY = (int)(height - (height * 1.0f / portion) * j);
			g.drawLine(49, valueY, 51, valueY);
			if (j % 2 == 1) {
				g.drawString(f.format(value), 0, valueY + 5);
			}
		}
 		
		// Draw data
 		int X,Y;
 		long simTime = simulator.getSimTime();
 		long timeWindow = simulator.getDynamicTimeWindow();
 		double ratioX = ((double)width) / timeWindow;
 		double ratioY = height / (rangeUpper - rangeLower);
 		
 		int fromIndex = Collections.binarySearch(timeList, simTime - timeWindow);
		int toIndex = Collections.binarySearch(timeList, simTime);
		if (fromIndex < 0) {
			// not found, convert to insertion point. @see Collections.binarySearch; 
			fromIndex = -fromIndex - 1;
		}
		if (toIndex < 0) {
			toIndex = -toIndex - 1;
		}
		
		List<Long> timeSubList = timeList.subList(fromIndex, toIndex);
		List<Double> dataSubList = dataList.subList(fromIndex, toIndex);
		g.setColor(Color.RED);
		for(int i = 0; i < timeSubList.size(); i++)
		{
			Long time = timeSubList.get(i);
			Double value = dataSubList.get(i);
			if (time == null) {
				throw new RuntimeException();
			}
			if (value == null) {
				continue;
			}
			X = (int)((time - simTime) * ratioX + width);
			Y = height - (int)((value - rangeLower) * ratioY);
			g.drawLine(X-1, Y, X+1, Y);
			g.drawLine(X, Y-1, X, Y+1);
		}
 	}

	public void setRangeUpper(double rangeUpper) {
		this.rangeUpper = rangeUpper;
	}

	public double getRangeUpper() {
		return rangeUpper;
	}

	public void setRangeLower(double rangeLower) {
		this.rangeLower = rangeLower;
	}

	public double getRangeLower() {
		return rangeLower;
	}
}
